[release/10.0] [Blazor] Fix metadata comments incorrectly being processed as logical elements #64530
+20
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #64529 to release/10.0
/cc @javiercn
Fix Blazor navigation error caused by metadata comments in logical tree
Prevent metadata comments from becoming logical elements during rendering
Description
This PR fixes a bug where Blazor metadata comments (such as WebAssembly options, component state, and initializers) were being converted into logical elements during the initial render. When these comments were later removed from the DOM during component discovery, they remained in the logical tree as orphaned references, causing
insertBeforeerrors during subsequent navigation between different render modes.The fix modifies
toLogicalElement()inLogicalElements.tsto skip metadata comments before they enter the logical tree, using a newisMetadataComment()helper function inComponentDescriptorDiscovery.tsthat identifies all Blazor metadata comment types.Fixes #64522, #64472
Customer Impact
Users experience JavaScript errors and navigation failures when navigating between pages with different render modes (InteractiveServer, InteractiveWebAssembly, or InteractiveAuto) in Blazor Web Apps. The error manifests as:
Uncaught TypeError: Cannot read properties of null (reading 'insertBefore')This particularly affects applications using:
PageTitlecomponents with different render modesRegression?
This is a regression introduced in .NET 10.0 when the WebAssembly options discovery mechanism was added. The
<!--Blazor-WebAssembly:{...}-->comment is new to v10.0 and was not present in v9.0. ThetoLogicalElement()function was indiscriminately converting all nodes including these new metadata comments into logical elements, which caused the orphaning issue when the comments were removed from the DOM.Risk
Justification:
isMetadataComment()function uses explicit prefix matching with well-defined comment formatsVerification
Manual verification completed:
BlazorApp10 (Issue .Net 10 Blazor Enhanced Navigation in WASM fails due to no PageTitle #64522): Validated navigation between InteractiveServer pages with/without PageTitle components, and InteractiveWebAssembly pages. Multiple navigation cycles completed without errors.
BugReportApp (Issue .NET 10 Blazor navigation from wasm to interactive server broken #64472): Validated navigation between InteractiveWebAssembly → InteractiveServer → InteractiveWebAssembly. Both directions work correctly with SignalR connections established properly.
Packaging changes reviewed?
This is a JavaScript/TypeScript code change only. No packaging changes required.
When servicing release/2.3